package com.example.livedatautilsdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.activity.ComponentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.livedatautilsdemo.helper.LiveDataHelper;
import com.example.livedatautilsdemo.helper.LiveDataTimeHelper;
import com.example.livedatautilsdemo.viewmodel.TimerLiveDataViewModel;

import java.util.Random;

/**
 * @author: njb
 * @date: 2023/7/31 0:14
 * @desc:
 */
public class MainActivity extends ComponentActivity {
    private TextView tvName;
    private TextView tvTime;
    private LiveDataHelper<String> mLiveDataHelper = new LiveDataHelper<>();
    private TimerLiveDataViewModel viewModel;
    private LiveDataTimeHelper liveDataTimeHelper;
    private Handler handler = new Handler(Looper.getMainLooper());

    private void updateCountDownUi(long seconds) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.d("LiveDataDemo", "onChanged: " + seconds);
                tvTime.setText(String.valueOf(seconds));
                if (seconds == 0) {
                    // 倒计时结束，执行相应操作
                    performAction();
                }
            }
        });
    }

    private void performAction() {
        viewModel.stopTiming();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initTime();
    }

    private void initTime() {
        viewModel = new ViewModelProvider(this).get(TimerLiveDataViewModel.class);
        liveDataTimeHelper = viewModel.getCurrentSecondLiveData();
        liveDataTimeHelper.observe(this, new Observer<Long>() {
            @Override
            public void onChanged(Long second) {
                updateCountDownUi(second);
            }
        });
        tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //InstallUtils.uninstall("com.example.windowdemo",MainActivity.this);
            }
        });
        viewModel.startTiming(10);
    }

    private void initData() {
        mLiveDataHelper.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String name) {
                Log.d("LiveDataDemo", "onChanged: " + name);
            }
        });

        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newName = "NewName" + new Random().nextInt(100);
                mLiveDataHelper.setValue(newName);
                Log.d("LiveDataDemo", "onClick: " + newName);
                tvName.setText(String.format("名称发生变化：%s", newName));
            }
        });
    }

    private void initView() {
        tvName = findViewById(R.id.textview);
        tvTime = findViewById(R.id.tvTime);
        tvTime.requestFocus();
        tvTime.setFocusable(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLiveDataHelper.call();
        liveDataTimeHelper.stopCountDown();
    }
}
